﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace Adams.Admin.DataAccess.DailyPrayers
{
    public static class CreatePrayer
    {
        public static Adams.Admin.DataTransfer.DailyPrayers.CreatePrayer.PageLoad PageLoad(int id, int month)
        {
            var data = new Adams.Admin.DataTransfer.DailyPrayers.CreatePrayer.PageLoad();
            
            using (SqlDataReader dr = DB.RunDataReader(@"
SELECT * FROM DailyPrayers.Locations WHERE LocationID = @LocationID
SELECT * FROM DailyPrayers.PrayerTimes WHERE LocationID = @LocationID AND Month = @Month
", CommandType.Text,
            DB.CreateParameter("@LocationID", id, SqlDbType.Int),
            DB.CreateParameter("@Month", month, SqlDbType.Int)))
            {
                data.Location = new Shared.Entities.DailyPrayers.Location();
                dr.Read();
                data.Location.ID = dr["LocationID"].DbInt();
                data.Location.Name = dr["Name"].DbString();
                data.Location.Latitude = dr["Latitude"].DbDecimal();
                data.Location.Longitude = dr["Longitude"].DbDecimal();
                data.Location.TimeZone = dr["TimeZone"].DbInt();

                dr.NextResult();
                data.Location.PrayerTimes = new Shared.Entities.DailyPrayers.PrayerTimeCollection();
                while (dr.Read())
                {
                    var prayer = new Shared.Entities.DailyPrayers.PrayerTime();
                    prayer.ID = dr["PrayerID"].DbInt();
                    prayer.Month = dr["Month"].DbInt();
                    prayer.IMonth = dr["IMonth"].DbInt();
                    prayer.Day = dr["Day"].DbInt();
                    prayer.IDay = dr["IDay"].DbInt();
                    prayer.Fajr = dr["Fajr"].DbString();
                    prayer.FajrIqama = dr["FajrIqama"].DbString();
                    prayer.Sunrise = dr["Sunrise"].DbString();
                    prayer.Duhr = dr["Duhr"].DbString();
                    prayer.DuhrIqama = dr["DuhrIqama"].DbString();
                    prayer.Asr = dr["Asr"].DbString();
                    prayer.AsrIqama = dr["AsrIqama"].DbString();
                    prayer.Maghrib = dr["Maghrib"].DbString();
                    prayer.MaghribIqama = dr["MaghribIqama"].DbString();
                    prayer.Isha = dr["Isha"].DbString();
                    prayer.IshaIqama = dr["IshaIqama"].DbString();
                    data.Location.PrayerTimes.Add(prayer);
                }
            }

            return data;
        }

        public static void Save(DataTransfer.DailyPrayers.CreatePrayer.FormInput input)
        {
            foreach (var prayertime in input.PrayerTimes)
            {
                DB.Run(@"
UPDATE DailyPrayers.PrayerTimes SET	IMonth = @IMonth, IDay = @IDay, Fajr = @Fajr, FajrIqama = @FajrIqama, Sunrise = @Sunrise,
	Duhr = @Duhr, DuhrIqama = @DuhrIqama, Asr = @Asr, AsrIqama = @AsrIqama,
	Maghrib = @Maghrib, MaghribIqama = @MaghribIqama, Isha = @Isha, IshaIqama = @IshaIqama
WHERE LocationID = @LocationID AND Month = @Month AND Day = @Day
IF(@@ROWCOUNT = 0)
BEGIN
	INSERT INTO DailyPrayers.PrayerTimes(LocationID, Month, IMonth, Day, IDay, Fajr, FajrIqama, Sunrise, Duhr, DuhrIqama, Asr, AsrIqama, Maghrib, MaghribIqama, Isha, IshaIqama)
	VALUES (@LocationID, @Month, @IMonth, @Day, @IDay, @Fajr, @FajrIqama, @Sunrise, @Duhr, @DuhrIqama, @Asr, @AsrIqama, @Maghrib, @MaghribIqama, @Isha, @IshaIqama)
END
", CommandType.Text,
                    DB.CreateParameter("@LocationID", input.LocationID, SqlDbType.Int),
                    DB.CreateParameter("@Month", prayertime.Month, SqlDbType.TinyInt),
                    DB.CreateParameter("@Day", prayertime.Day, SqlDbType.TinyInt),
                    DB.CreateParameter("@IMonth", prayertime.IMonth, SqlDbType.TinyInt),
                    DB.CreateParameter("@IDay", prayertime.IDay, SqlDbType.TinyInt),
                    DB.CreateParameter("@Fajr", string.IsNullOrEmpty(prayertime.Fajr) ? "": prayertime.Fajr, SqlDbType.VarChar, 50),
                    DB.CreateParameter("@FajrIqama", string.IsNullOrEmpty(prayertime.FajrIqama) ? "" : prayertime.FajrIqama, SqlDbType.VarChar, 50),
                    DB.CreateParameter("@Sunrise", string.IsNullOrEmpty(prayertime.Sunrise) ? "" : prayertime.Sunrise, SqlDbType.VarChar, 50),
                    DB.CreateParameter("@Duhr", string.IsNullOrEmpty(prayertime.Duhr) ? "" : prayertime.Duhr, SqlDbType.VarChar, 50),
                    DB.CreateParameter("@DuhrIqama", string.IsNullOrEmpty(prayertime.DuhrIqama) ? "" : prayertime.DuhrIqama, SqlDbType.VarChar, 50),
                    DB.CreateParameter("@Asr", string.IsNullOrEmpty(prayertime.Asr) ? "" : prayertime.Asr, SqlDbType.VarChar, 50),
                    DB.CreateParameter("@AsrIqama", string.IsNullOrEmpty(prayertime.AsrIqama) ? "" : prayertime.AsrIqama, SqlDbType.VarChar, 50),
                    DB.CreateParameter("@Maghrib", string.IsNullOrEmpty(prayertime.Maghrib) ? "" : prayertime.Maghrib, SqlDbType.VarChar, 50),
                    DB.CreateParameter("@MaghribIqama", string.IsNullOrEmpty(prayertime.MaghribIqama) ? "" : prayertime.MaghribIqama, SqlDbType.VarChar, 50),
                    DB.CreateParameter("@Isha", string.IsNullOrEmpty(prayertime.Isha) ? "" : prayertime.Isha, SqlDbType.VarChar, 50),
                    DB.CreateParameter("@IshaIqama", string.IsNullOrEmpty(prayertime.IshaIqama) ? "" : prayertime.IshaIqama, SqlDbType.VarChar, 50));
            }
        }
    }
}
